﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using System.Data;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Customer.Manage
{
    public partial class Redeem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUtility.UserGUID == null || WebUtility.UserGUID == "0")
            {
                Response.Clear();
                Response.Write("<script>parent.ResetURL('/Customer/signin.aspx?next=" + Server.UrlEncode("/Customer/Index.aspx") + "');</script>");
                Response.End();
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        void BindData()
        {
            CouponRedeemDAL redeem = new CouponRedeemDAL();
            DataTable dt = redeem.GetByUserGUID(WebUtility.UserGUID);
            DataView dv = new DataView(dt);
            dv.Sort = GetSortBy();
            GridView_Redeem.DataSource = dv;
            GridView_Redeem.DataBind();
        }

        protected void btn_Apply_Click(object sender, EventArgs e)
        {
            CouponRedeemDAL redeem = new CouponRedeemDAL();
            CouponRedeemInfo info = redeem.GetByCouponCode(tb_CouponCode.Text.Trim());

            if (info != null)
            {
                if (info.IsRedeemed == 0)
                {
                    info.IsRedeemed = 1;
                    info.RedeemDate = DateTime.Now;

                    redeem.Update(info);
                    lb_Msg.Text = "<div class='alert alert-success'><img src=" + info.CouponCodeLocation.ToString() + "></img><h2>Verified.</h2></div>";
                }
                else if (info.IsRedeemed == 1)
                {
                    lb_Msg.Text = "<div class='alert alert-error'><h2>This code has been redeemed already.</h2></div>";
                }
            }
            else
            {
                lb_Msg.Text = "<div class='alert alert-error'>Failed</div>";
            }

            BindData();
        }

        protected void GridView_Redeem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Redeem.PageIndex = e.NewPageIndex;

            CouponRedeemDAL redeem = new CouponRedeemDAL();
            DataTable dt = redeem.GetByUserGUID(WebUtility.UserGUID);
            DataView dv = new DataView(dt);
            dv.Sort = GetSortBy();
            GridView_Redeem.DataSource = dv;
            GridView_Redeem.DataBind();
        }

        protected void GridView_Redeem_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Session["SortDirection"] == null) Session["SortDirection"] = "ASC";
            if (Session["SortBy"] == null) Session["SortBy"] = "ListID";
            if (e.SortExpression == Session["SortBy"].ToString()) Session["SortDirection"] = Session["SortDirection"] == "ASC" ? "DESC" : "ASC";
            else Session["SortDirection"] = "ASC";

            CouponRedeemDAL redeem = new CouponRedeemDAL();
            DataTable dt = redeem.GetByUserGUID(WebUtility.UserGUID);
            DataView dv = new DataView(dt);
            Session["SortBy"] = e.SortExpression;
            dv.Sort = GetSortBy();
            GridView_Redeem.DataSource = dv;
            GridView_Redeem.DataBind();
        }

        string GetSortBy()
        {
            string sortby = "";

            sortby = " ListID";

            if (Session["SortBy"] != null)
            {
                if (!string.IsNullOrEmpty(Session["SortBy"].ToString()))
                {
                    sortby = " " + Session["SortBy"].ToString();
                }
            }
            else Session["SortBy"] = sortby;

            if (Session["SortDirection"] != null)
            {
                if (!string.IsNullOrEmpty(Session["SortDirection"].ToString()))
                {
                    return sortby + " " + Session["SortDirection"].ToString();
                }
            }

            return sortby + " " + "ASC";
        }

        public string GetStatus(object isredeemed)
        {
            if (isredeemed.ToString() == "1") return "<span class='label label-success'>Redeemed</span>";
            return "<span class='label'>Pending</span>";
        }

        public string FormatDate(object _d)
        {
            DateTime? dt = DataHelper.GetDateTime(_d);
            if (dt != null)
            {
                DateTime d = dt.Value;

                string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
                if (d.Month.ToString().Length < 2) month = "0" + d.Month;
                if (d.Day.ToString().Length < 2) day = "0" + d.Day;
                return year + "-" + month + "-" + day;
            }
            return "";
        }

        public string FormatShortDate(object _d)
        {
            DateTime? dt = DataHelper.GetDateTime(_d);
            if (dt != null)
            {
                DateTime d = dt.Value;

                string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
                return month + "/" + day;
            }
            return "";
        }
    }
}